﻿using CommonTool;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AutoMachine;
using Tool;
using System.Windows.Forms;
using Motions;

namespace CommonTool
{

    /// <summary>
    /// Bit
    /// </summary>
    public enum SysBit
    {
        //************************设备整体******************************//

        HpmeOKAll,
        //************************工位屏蔽*********************************//
        左工位屏蔽,
        上下弹夹工站屏蔽,
        弹夹拆分工站屏蔽,
        载片中转工站屏蔽,
        中转搬运定位工站屏蔽,
        中转取料工站屏蔽,
        飞拍1号工站屏蔽,
        中转搬运工站屏蔽,
        飞拍2号工站屏蔽,
        飞拍3号工站屏蔽,
        飞拍后取料工站屏蔽,
        取料分OKNG工站屏蔽,
        OK满料后下料工站屏蔽,
        OK弹夹合并工站屏蔽,
        //************************工位动作完成*********************************//
 
        UpAndDownLoadUp1HaveOK,
        UpAndDownLoadUp2HaveOK,
        UpAndDownLoadDown1HaveOK,
        UpAndDownLoadDown2HaveOK,
        上下弹夹工站通知弹夹拆分工站完成,
        上下弹夹工站通知弹合并分工站完成,
        上下弹夹工站通知弹合并分工站强制满料,
        弹夹拆分工站通知中转搬运定位工站完成,
        弹夹拆分工站通知上下弹夹工站完成,
        载片中转工站通知中转搬运定位工站完成,
        载片中转工站通知OK满料后下料工站完成,
        中转搬运定位工站通知载片中转工站完成,
        中转搬运定位工站通知弹夹拆分工站完成,
        中转搬运定位工站通知中转取料工站完成,
        中转取料工站通知中转搬运定位工站完成,
        中转取料工站通知飞拍1号工站完成,
        飞拍1号工站通知中转取料工站完成,
        飞拍1号工站通知中转搬运工站完成,
        中转搬运工站通知飞拍1号工站完成,
        中转搬运工站通知飞拍2号工站完成,
        中转搬运工站通知飞拍2号工站放料完成,
        中转搬运工站通知飞拍3号工站完成,
        中转搬运工站通知飞拍3号工站放料完成,
        飞拍2号工站安全位,
        飞拍2号工站通知中转搬运工站完成,
        飞拍2号工站通知中转搬运工站可以放料,
        飞拍2号工站通知飞拍后取料工站完成,
        飞拍3号工站安全位,
        飞拍3号工站通知中转搬运工站完成,
        飞拍3号工站通知中转搬运工站可以放料,
        飞拍3号工站通知飞拍后取料工站完成,
        双工位飞拍测试中,
        飞拍后取料工站通知双飞拍工站完成,
        飞拍后取料工站通知取料分OKNG完成,
        取料分OKNG工站通知完飞拍后取料工站成,
        取料分OKNG工站通知OK满料后下料工站完成,
        OK满料后下料工站通知取料分OKNG工站完成,
        OK满料后下料工站通知OK弹夹合并工站完成,
        OK满料后下料工站通知载片中转工站完成,
        OK弹夹合并工站通知OK满料后下料工站完成,
        OK弹夹合并工站通知上下弹夹工站完成,
    }
    /// <summary>
    /// Int
    /// </summary>
    public enum SysInt
    {
        intStep,
        UpAndDownLoadStep,
        MagSplitStep,
        SlidesTransitStep,
        TransitCarryLocationStep,
        TransitPickUpStep,
        FlyFilming1Step,
        TransitCarryStep,
        FlyFilming2Step,
        FlyFilming3Step,
        FlyAfterPickUpStep,
        PutDownOKNGStep,
        OKFullStep,
        OKMagMergeStep,
        /// <summary>
        /// 定位取料工位取料次数
        /// </summary>
        PickUpTimes
            
    }
    public class SystemMgr : SingletonTemplate<SystemMgr>
    {

        /// <summary>
        /// 构造函数 初始化内存区
        /// </summary>
        public SystemMgr()
        {
            this.m_bitArray = new bool[1024];
            this.m_intArray = new int[1024];
            this.m_doubleArray = new double[1024];
            this.m_bitArray.Initialize();
            this.m_intArray.Initialize();
            this.m_doubleArray.Initialize();
        }

        private bool[] m_bitArray;//Bit

        private int[] m_intArray;//Int

        private double[] m_doubleArray;//Double

        #region 定义位
        //定义一个Bit位事件
        public event SystemMgr.BitChangedHandler BitChangedEvent;

        //定义一个Bit位的委托
        public delegate void BitChangedHandler(int nIndex, bool bBit);

        /// <summary>
        ///向指定的位写值
        /// </summary>
        /// <param name="nIndex">Bit位</param>
        /// <param name="bBit">True Or False</param>
        public void WriteSysBit(SysBit nIndex, bool bBit)
        {
            this.m_bitArray[(int)nIndex] = bBit;
            if (this.BitChangedEvent != null)
            {
                this.BitChangedEvent((int)nIndex, bBit);
            }

        }

        #endregion

        #region 定义Int
        //定义一个整形写入值的事件
        public event SystemMgr.IntChangedHandler IntChangedEvent;

        //定义一个整形写入值的委托
        public delegate void IntChangedHandler(int nIndex, int nData);

        /// <summary>
        /// 向指定的Int写值
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="nData"></param>
        public void WriteSysInt(SysInt nIndex, int nData)
        {
            this.m_intArray[(int)nIndex] = nData;
            if (this.IntChangedEvent != null)
            {
                this.IntChangedEvent((int)nIndex, nData);
            }

        }

        #endregion

        #region 定义Double

        //定义一个浮点数写入值的事件
        public event SystemMgr.DoubleChangedHandler DoubleChangedEvent;

        //定义一个浮点数写入值的委托
        public delegate void DoubleChangedHandler(int nIndex, double fdata);


        /// <summary>
        /// 向指定的Double写值
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="fdata"></param>
        public void WriteSysDouble(int nIndex, double fdata)
        {
            this.m_doubleArray[nIndex] = fdata;
            if (this.DoubleChangedEvent != null)
            {
                this.DoubleChangedEvent(nIndex, fdata);
            }
        }

        #endregion

        /// <summary>
        /// 获取指定的位寄存器状态
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public bool GetSysBit(SysBit nIndex)
        {
            return this.m_bitArray[(int)nIndex];
        }

        /// <summary>
        /// 获取指定的整形寄存器的值
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public int GetSysInt(int nIndex)
        {

            return this.m_intArray[nIndex];
        }

        /// <summary>
        /// 获取指定的浮点型寄存器的值
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public double GetSysDouble(int nIndex)
        {

            return this.m_doubleArray[nIndex];
        }

        /// <summary>
        /// 等待指定位寄存器有效
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="bit">On Off</param>
        public void WaitSysBit(SysBit nIndex, bool bit)
        {
            LogView.GetInstance().ShowLog(LogView.LogType.Oper, "等待" + Enum.GetName(typeof(SysBit), nIndex) + " " + Convert.ToString(bit));
            Line1:
            DateTime now = DateTime.Now;
            while (true)
            {
                bool a = SystemMgr.GetInstance().GetSysBit(nIndex);
                if (a == bit)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(10);
                    if ((DateTime.Now - now).Seconds > Convert.ToInt16 (SystemCfg.Param["SysBitWaitTime"].Value.ToString()))//1分钟
                    {
                        DialogResult result = MessageBox.Show(Enum.GetName(typeof(SysBit), nIndex) + "  " + "位信号", "超时提醒", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                        if (result == DialogResult.Abort)
                        {
                            //终止
                            Device.ChangeStateMode(Device.STATEMode.PAUSE);//切换暂停                      
                            break;
                        }
                        else if (result == DialogResult.Retry)
                        {
                            //重试
                            goto Line1;//重置超时
                        }
                        else if (result == DialogResult.Ignore)
                        {
                            //忽略
                            break;
                        }
                    }
                }
                Thread.Sleep(5);
            }

        }

        /// <summary>
        /// 等待指定整形寄存器值有效
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="num"></param>
        public void WaitSysInt(int nIndex, int num)
        {
            Line1:
            DateTime now = DateTime.Now;
            while (true)
            {
                int a = SystemMgr.GetInstance().GetSysInt(nIndex);
                if (a == num)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(10);
                    if ((DateTime.Now - now).Seconds > Convert.ToInt16(SystemCfg.Param["SysBitWaitTime"].Value.ToString()))//1分钟
                    {
                        DialogResult result = MessageBox.Show(Enum.GetName(typeof(SysInt), nIndex) + "  " + "整形寄存器信号", "超时提醒", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                        if (result == DialogResult.Abort)
                        {
                            //终止
                            Device.ChangeStateMode(Device.STATEMode.PAUSE);//切换暂停                      
                            break;
                        }
                        else if (result == DialogResult.Retry)
                        {
                            //重试
                            goto Line1;//重置超时
                        }
                        else if (result == DialogResult.Ignore)
                        {
                            //忽略
                            break;
                        }
                    }
                }
                Thread.Sleep(5);
            }




        }

        /// <summary>
        /// 等待指定浮点型寄存器值有效
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="num"></param>
        public void WaitSysDouble(int nIndex, int num)
        {
            Line1:
            DateTime now = DateTime.Now;
            while (true)
            {
                double a = SystemMgr.GetInstance().GetSysDouble(nIndex);
                if (a == num)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(10);
                    if ((DateTime.Now - now).Seconds > Convert.ToInt16(SystemCfg.Param["SysBitWaitTime"].Value.ToString()))//1分钟
                    {
                        DialogResult result = MessageBox.Show(Enum.GetName(typeof(Double), nIndex) + "  " + "浮点型寄存器信号", "超时提醒", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                        if (result == DialogResult.Abort)
                        {
                            //终止
                            Device.ChangeStateMode(Device.STATEMode.PAUSE);//切换暂停                            
                            break;
                        }
                        else if (result == DialogResult.Retry)
                        {
                            //重试
                            goto Line1;//重置超时
                        }
                        else if (result == DialogResult.Ignore)
                        {
                            //忽略
                            break;
                        }
                    }
                }
                Thread.Sleep(5);
            }



        }

        /// <summary>
        /// 延时时间
        /// </summary>
        /// <param name="time">秒数</param>
        //public void WaitDelayTime(int time)
        //{
        //    DateTime now = DateTime.Now;
        //    while (true)
        //    {
        //        if ((DateTime.Now - now).Milliseconds >= time)//
        //        {
        //            break;
        //        }
        //    }
        //}

        //public void Rundelay(double ti)//延时 单位毫秒
        //{
        //    DateTime now = DateTime.Now;
        //    while (true)
        //    {
        //        if ((DateTime.Now - now).Seconds >= ti)//
        //        {
        //            break;
        //        }
        //    }
        //}

        //构建延时函数
        //public void Delay(double delayTime)
        //{
        //    DateTime now = DateTime.Now;
        //    double s;
        //    do
        //    {
        //        TimeSpan spand = DateTime.Now - now;
        //        s = spand.Milliseconds;  //Milliseconds是指以毫秒计数
        //        Application.DoEvents();
        //    }
        //    while (s < delayTime);
        //}

        ///// <summary>
        ///// 延时等待
        ///// </summary>
        ///// <param name="ti">等待时间，单位秒</param>
        //public void Rundelay(double ti)//延时 单位毫秒
        //{
        //    DateTime temptime = DateTime.Now;
        //    while (temptime.AddMilliseconds(ti).CompareTo(DateTime.Now) > 0)
        //    {
        //        Application.DoEvents();
        //    }
        //}

        public void Delay(int milliSecond)
        {
            int start = Environment.TickCount;
            while (Math.Abs(Environment.TickCount - start) < milliSecond)
            {
                Application.DoEvents();
            }
        }

      
        /// <summary>
        ///等待设备为运行状态
        /// </summary>
        public void WaitRun()
        {
            if (GlobalVar.OneStep == true)//单步模式
            {
                while (true)
                {
                    Thread.Sleep(10);
                    if (MotionMgr.GetInstance().ReadIn("启动按钮") == true)
                    {
                        break;
                    }
                }
            }
            else
            {
                while (true)
                {
                    Thread.Sleep(10);
                    if (Device.StateMode == Device.STATEMode.RUN)
                    {
                        break;
                    }
                }
            }



        }

        ///// <summary>
        ///// 单步模式
        ///// </summary>
        //public void WaitOneStep()
        //{
        //    if (GloSta.OneStep == true)//单步模式
        //    {
        //        while (true)
        //        {
        //            Thread.Sleep(10);
        //            if (MotionMgr.GetInstance().Card.ReadIn(SystemCfg.In["启动按钮"].Index) == true)
        //            {
        //                break;
        //            }
        //        }

        //    }
        //    //else
        //    //{
        //    //    return;
        //    //}



        //}


    



        /// <summary>
        /// Double
        /// </summary>
        public enum SysDouble
        {
            百分比1,
            百分比2,
        }


    }
}
